package com.aaa.controller;

import com.aaa.entity.*;
import com.aaa.service.RoleService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author ：dudu（12232347710@qq.com）
 * @date ：Created in 2022/6/28 17:25
 * @description：
 * @modified By：
 */
@Controller
@RequestMapping("role")
public class RoleController extends BaseController{
    @Autowired
    private RoleService roleService;
    MyReturn myReturn=new MyReturn();
    int num=0;
    @RequestMapping("roleUser.do")
    public String Pages(){
        return "roleUser";
    }
    /**
     * @author     ：dudu
     * @Description: 查询所有并排序
    */
    @RequestMapping("findAllRole.do")
    @ResponseBody
    public MyReturn findAllRole(PageSort pageSort){
        Page allRole=roleService.findAllRole(pageSort);
        myReturn.setCount(allRole.getTotal());
        myReturn.setData(allRole.getResult());
        return super.getSuccessResult(myReturn);
    }
    /**
     * @author     ：dudu
     * @Description: 添加
    */
    @RequestMapping("addAllRole.do")
    @ResponseBody
    public MyReturn addAllRole(Role role,String menus){
        Role insert = roleService.addRole(role);
        insert.setCreateTime(new Date());
        if (insert != null) {
            String[] menusId = menus.split(",");
            roleService.insertRoleMenu(insert.getRoleId(), menusId);
            return getSuccessResult(myReturn);
        }
        return getFailResult(myReturn);
    }
    @RequestMapping("deleteRole.do")
    @ResponseBody
    public MyReturn deleteRole(Integer roleId) {
        boolean deleteById = roleService.deleteById(roleId);
        if (deleteById){
            return super.getSuccessResult(myReturn);
        }else {
            return super.getFailResult(myReturn);
        }
    }
    /**
     * @author     ：dudu
     * @Description: 修改
    */
    @RequestMapping ("updateRole.do")
    @ResponseBody
    public MyReturn updateRole(Role role, String menus) {
        Role update = roleService.updateRole(role);
        update.setUpdateTime(new Date());
        if (update != null) {
            String[] menusId = menus.split(",");
            roleService.updateRoleMenu(update.getRoleId(), menusId);
            return getSuccessResult(myReturn);
        }
        return getFailResult(myReturn);
    }
    /**
     * @param :
     * @return java.lang.Object
     * @create by: IvanZ
     * @description : 获取树形菜单数据
     * @create time: 2020/12/20 1:12
     */
    @RequestMapping ("selectAllMenu.do")
    @ResponseBody
    public List<LayUiTree> selectAllMenu() {
        List<Menu> menus = roleService.selectAllMenu();

        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId() == 0) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setUrl(menu.getUrl());
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                //组件展开
//                tree.setSpread(true);
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }
    @RequestMapping ("findSelectMenu.do")
    @ResponseBody
    public List<Integer> findSelectMenu(int roleId) {
        return roleService.findSelectMenu(roleId);
    }
    /**
     * @author     ：dudu
     * @Description: 递归得到孩子节点
    */
    public List<LayUiTree> getChildren(Integer parentId, List<Menu> menus) {
        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId().equals(parentId)) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setUrl(menu.getUrl());
                //组件展开
//                tree.setSpread(true);
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }
}
